﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EShop.BLL;
using EShop.Model;
using EShop.Common;
namespace EShop.Client
{
    public partial class frmMerchandiseToSell : frmBase
    {
        public frmMerchandiseToSell()
        {
            InitializeComponent();
        }

        private void frmMerchandiseToSell_Load(object sender, EventArgs e)
        {
            BindStockData();
        }

        private void BindStockData()
        {
            DataTable dt = new MerchandiseSellBLL().GetStockToSellData();
            int iCurrentRow=0;
            foreach (DataRow item in dt.Rows)
            {
                iCurrentRow = dgv_StockList.Rows.Add();
                dgv_StockList.Rows[iCurrentRow].Cells["barcode"].Value = item["barcode"].ToString();
                dgv_StockList.Rows[iCurrentRow].Cells["MerchandiseName"].Value = item["MerchandiseName"].ToString();
                try
                {
                    dgv_StockList.Rows[iCurrentRow].Cells["SellPrice"].Value = FormatManager.FormatDecimal((decimal)item["SellPrice"]);
                    dgv_StockList.Rows[iCurrentRow].Cells["SalePrice"].Value = FormatManager.FormatDecimal((decimal)item["SalePrice"]);
                }
                catch 
                {
                    dgv_StockList.Rows[iCurrentRow].Cells["SellPrice"].Value = 0;
                    dgv_StockList.Rows[iCurrentRow].Cells["SalePrice"].Value = 0;
                }
            }

            if (dgv_StockList.Rows.Count > 0)
                dgv_StockList.Rows[0].Selected = false;

        }

        private void dgv_StockList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_StockList.CurrentRow != null)
            {
                DataGridViewRow row = dgv_StockList.CurrentRow;
                txtBarcode.Text = row.Cells["Barcode"].Value.ToString();
                txtMerchandiseName.Text = row.Cells["MerchandiseName"].Value.ToString();
                txtSellPrice.Text = row.Cells["SellPrice"].Value.ToString();
                txtSalePrice.Text = row.Cells["SalePrice"].Value.ToString();
                btnToSell.Enabled = true;
            }
        }

        private void btnToSell_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSellPrice.Text))
            {
                //请插入上架价格
                MessageBox.Show("请填写上架的销售价格");
                return;
            }
            if (string.IsNullOrEmpty(txtSalePrice.Text))
            {
                MessageBox.Show("请填写上架的打折价格");
                return;
            }
            MerchandiseSell oMerchandiseSell = new MerchandiseSell();
            oMerchandiseSell.Barcode = txtBarcode.Text;
            try
            {
                oMerchandiseSell.SellPrice = Convert.ToDecimal(txtSellPrice.Text);
            }
            catch
            {
                MessageBox.Show("请输入正确的价格", "输入的价格不正确");
                this.txtSellPrice.SelectAll();
                this.txtSellPrice.Focus();
                return;
            }
            try
            {
                oMerchandiseSell.SalePrice = Convert.ToDecimal(txtSalePrice.Text);
            }
            catch
            {
                MessageBox.Show("请输入正确的价格", "输入的价格不正确");
                this.txtSalePrice.SelectAll();
                this.txtSalePrice.Focus();
                return;
            }
            bool falg = new MerchandiseSellBLL().AddMerchandiseSell(oMerchandiseSell);
            if (falg)
            {
                MessageBox.Show("上架成功!");
                dgv_StockList.Rows.Clear();
                BindStockData();
            }
            else
            {
                MessageBox.Show("上架失败，请联系管理员");
            }
        }

        private void txtSellPrice_TextChanged(object sender, EventArgs e)
        {
            try
            {
                decimal d = Convert.ToDecimal(txtSellPrice.Text);
            }
            catch
            {
                txtSellPrice.Text = "1.00";
            }
        }

        private void txtSalePrice_TextChanged(object sender, EventArgs e)
        {
            try
            {
                decimal d = Convert.ToDecimal(txtSalePrice.Text);
            }
            catch
            {
                txtSalePrice.Text = "1.00";
            }
        }

        private void frmMerchandiseToSell_FormClosing(object sender, FormClosingEventArgs e)
        {
            dgv_StockList.Dispose();
        }
    }
}
